package nero.yncc.com.createart.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanShopCar;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.bean.UserBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.fragment.FragmentShopcar;
import nero.yncc.com.createart.interfaces.InterfaceDoSomeThing;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.ShopCarUtil;
import nero.yncc.com.createart.utils.SomeUtil;
import nero.yncc.com.createart.utils.TextUtil;

import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

/**
 * Created by nero on 17-6-23.
 */


public class AdapterShopCarOutSize extends RecyclerView.Adapter<AdapterShopCarOutSize.Holder> {

    Context context;
    ArrayList<BeanShopCar> arrayList;
    UserBean userBean;
    Activity activity;
    InterfaceDoSomeThing doSomeThing;
    boolean canEdit = true;

    public AdapterShopCarOutSize(Activity activity, Context context, ArrayList<BeanShopCar> arrayList, UserBean userBean, boolean canEdit, InterfaceDoSomeThing doSomeThing) {
        this.context = context;
        this.arrayList = arrayList;
        this.userBean = userBean;
        this.activity = activity;
        this.doSomeThing = doSomeThing;
        this.canEdit = canEdit;
    }


    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_shopcar_outside, null);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final BeanShopCar beanShopCar = arrayList.get(position);
        String name = beanShopCar.getName();
        switch (name) {
            case "组合套装":
            case "场景套装":
                holder.ll_all.setVisibility(VISIBLE);
                holder.ll_single.setVisibility(GONE);
                if (name.equals("组合套装")) {
                    holder.tv_name_all.setText("组合");
                    holder.tv_name2.setText("组合套装");
                } else {
                    holder.tv_name_all.setText("场景");
                    holder.tv_name2.setText("场景套装");
                }


                final ArrayList<BeanShopCar.goods> arrayList_goods = beanShopCar.getList();
                double price = 0, price_market = 0, sheng = 0;

                for (BeanShopCar.goods goods : arrayList_goods) {
                    price += Double.parseDouble(goods.getShop_price()) * goods.getNumber();
                    price_market += Double.parseDouble(goods.getMarket_price()) * goods.getNumber();
                }
                if (beanShopCar.isChoose()) {
                    holder.icon_choose_all.setTextColor(context.getResources().getColor(R.color.project_yellow));
                } else {
                    holder.icon_choose_all.setTextColor(context.getResources().getColor(R.color.lineColorGray));
                }

                holder.icon_choose_all.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        beanShopCar.setChoose(!beanShopCar.isChoose());
                        AdapterShopCarOutSize.this.notifyDataSetChanged();
                    }
                });
                sheng = price_market - price;
                beanShopCar.setPrice(price);
                beanShopCar.setPrice_market(price_market);
                beanShopCar.setSheng(sheng);
                holder.tv_price_all.setText("￥" + price);
                holder.tv_price_market.setText("￥" + price_market);
                holder.tv_price_market.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                holder.tv_price_all.setText("￥" + price);
                holder.tv_sheng.setText("￥" + sheng);

                //如果是订单页面的 那就隐藏选择框
                if (!canEdit)
                    holder.icon_choose_all.setVisibility(GONE);

                if (beanShopCar.isEdit()) {
                    holder.tv_del_top.setVisibility(VISIBLE);
                    holder.tv_sheng.setVisibility(GONE);
                } else {
                    holder.tv_del_top.setVisibility(GONE);
                    holder.tv_sheng.setVisibility(VISIBLE);
                }

                holder.tv_del_top.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        doSomeThing.doSomething(beanShopCar.getId());
                    }
                });

                AdapterShopCarRecylerView adapter_shopcar = new AdapterShopCarRecylerView(context, beanShopCar, doSomeThing,canEdit);
                holder.rv_list.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
                holder.rv_list.setAdapter(adapter_shopcar);
                break;
            case "单商品":
                holder.ll_all.setVisibility(View.GONE);
                holder.ll_single.setVisibility(VISIBLE);

                final BeanShopCar.goods godBean = beanShopCar.getList().get(0);
                holder.tv_price.setText("￥" + TextUtil.getText(godBean.getShop_price(), "0") + "");
                if (beanShopCar.isChoose()) {
                    holder.icon_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
                } else {
                    holder.icon_choose.setTextColor(context.getResources().getColor(R.color.lineColorGray));
                }
                holder.tv_name.setText(TextUtil.getText(godBean.getGoods_name(), "暂无名称"));
                holder.tv_shuxing.setText(TextUtil.getText(godBean.getSpec_name(), "暂无简介"));
                //点击选中
                holder.icon_choose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        beanShopCar.setChoose(!beanShopCar.isChoose());
                        AdapterShopCarOutSize.this.notifyDataSetChanged();
                        doSomeThing.doSomething();
                    }
                });

                if (beanShopCar.isEdit()) {
                    holder.tv_del.setVisibility(VISIBLE);
                    holder.tv_add.setVisibility(GONE);
                    holder.tv_cut.setVisibility(GONE);
                    holder.tv_num.setVisibility(GONE);
                } else {
                    holder.tv_del.setVisibility(GONE);
                    holder.tv_add.setVisibility(VISIBLE);
                    holder.tv_cut.setVisibility(VISIBLE);
                    holder.tv_num.setVisibility(VISIBLE);
                }
                if(!canEdit){
                    holder.tv_add.setVisibility(GONE);
                    holder.tv_cut.setVisibility(GONE);
                    holder.tv_num.setVisibility(GONE);
                    holder.tv_del.setVisibility(GONE);
                    holder.icon_choose.setVisibility(GONE);
                }
                holder.tv_del.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        doSomeThing.doSomething(beanShopCar.getId());
                    }
                });
//                    //点击选中
//                    holder.ll_root.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            godBean.setChoose(!godBean.isChoose());
//                            AdapterShopCarOutSize.this.notifyDataSetChanged();
//                            fragmentShopcar.setMoney();
//                        }
//                    });


                //减少数量
                holder.tv_cut.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //减少
                        try {
                            int num = godBean.getNumber();
                            if (num > 1) {
                                //获取传递加减字符串

//                                String content="{\"goods_id\":"+godBean.getGoods_id()+", \"spec_list\":[{\"spec_id\":35, \"item_id\":155}],\"price\":100,\"market_price\":250,\"number\":1}";

//                                ShopCarUtil.changeShopCarNum(activity,userBean,beanShopCar.getId(),);
                                num--;
                                godBean.setNumber(num);
                                holder.tv_num.setText(num + "");
                                holder.tv_xnum.setText("x " + num);
                                beanShopCar.setChoose(true);
                                doSomeThing.doSomething();
                            }

                        } catch (Exception e) {
                        }
                    }
                });
                //增加数量
                holder.tv_add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //减少
                        try {
                            int num = godBean.getNumber();

                            num++;
                            godBean.setNumber(num);


                            holder.tv_num.setText(num + "");
                            holder.tv_xnum.setText("x " + num);
                            beanShopCar.setChoose(true);
                            doSomeThing.doSomething();
                        } catch (Exception e) {

                        }
                    }
                });

                ImageUtils.BindImage(context, holder.iv_image, godBean.getOriginal_img(), 2);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView tv_name, tv_name2, tv_price, tv_price_market, tv_sheng, tv_name_all, tv_price_all;
        RecyclerView rv_list;
        IconView icon_choose, icon_choose_all;
        ImageView iv_image;
        TextView tv_shuxing;
        TextView tv_cut;
        TextView tv_num;
        TextView tv_add;
        TextView tv_del;
        TextView tv_xnum, tv_del_top;


        RelativeLayout ll_single;
        LinearLayout ll_all;

        public Holder(View itemView) {
            super(itemView);

            ll_all = itemView.findViewById(R.id.ll_all);
            tv_name_all = itemView.findViewById(R.id.tv_name_all);
            tv_price_all = itemView.findViewById(R.id.tv_price_all);
            ll_single = itemView.findViewById(R.id.ll_single);

            tv_name2 = itemView.findViewById(R.id.tv_name2);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_price_market = itemView.findViewById(R.id.tv_price_market);
            tv_sheng = itemView.findViewById(R.id.tv_sheng);
            rv_list = itemView.findViewById(R.id.rv_list);
            icon_choose = itemView.findViewById(R.id.icon_choose);
            icon_choose_all = itemView.findViewById(R.id.icon_choose_all);

            iv_image = itemView.findViewById(R.id.iv_image);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_shuxing = itemView.findViewById(R.id.tv_shuxing);
            tv_cut = itemView.findViewById(R.id.tv_cut);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_add = itemView.findViewById(R.id.tv_add);
            tv_del = itemView.findViewById(R.id.tv_del);
            tv_xnum = itemView.findViewById(R.id.tv_xnum);

            tv_del_top = itemView.findViewById(R.id.tv_del_top);
        }
    }

}

//卡顿!!卡顿!!卡顿! 重要的事情说三遍
//
//public class AdapterShopCarOutSize extends BaseAdapter {
//
//    Context context;
//    ArrayList<BeanShopCar> arrayList;
//
//    FragmentShopcar fragmentShopcar;
//    Activity activity;
//
//    public AdapterShopCarOutSize(  Activity activity,Context context, ArrayList<BeanShopCar> arrayList, FragmentShopcar fragmentShopcar) {
//        this.context = context;
//        this.arrayList = arrayList;
//        this.activity=activity;
//        this.fragmentShopcar = fragmentShopcar;
//    }
//
//    @Override
//    public int getCount() {
//        return arrayList.size();
//    }
//
//    @Override
//    public Object getItem(int i) {
//        return i;
//    }
//
//    @Override
//    public long getItemId(int i) {
//        return i;
//    }
//
//    @Override
//    public View getView(int i, View view, ViewGroup viewGroup) {
//        final Holder holder;
//        final BeanShopCar beanShopCar = arrayList.get(i);
//        String name = beanShopCar.getName();
//        if (view == null) {
//            view = LayoutInflater.from(context).inflate(R.layout.adapter_shopcar_outside, null);
//            holder = new Holder(view);
//            view.setTag(holder);
//        } else {
//            holder = (Holder) view.getTag();
//        }
//
//        switch (name) {
//            case "组合套装":
//            case "场景套装":
//                holder.ll_all.setVisibility(VISIBLE);
//                holder.ll_single.setVisibility(GONE);
//                if (name.equals("组合套装")) {
//                    holder.tv_name_all.setText("组合");
//                    holder.tv_name2.setText("组合套装");
//                } else {
//                    holder.tv_name_all.setText("场景");
//                    holder.tv_name2.setText("场景套装");
//                }
//
//
//                final ArrayList<BeanShopCar.goods> arrayList_goods = beanShopCar.getList();
//                double price = 0, price_market = 0, sheng = 0;
//
//                for (BeanShopCar.goods goods : arrayList_goods) {
//                    if (goods.isChoose()) {
//                        price += Double.parseDouble(goods.getShop_price()) * goods.getNumber();
//                        price_market += Double.parseDouble(goods.getMarket_price()) * goods.getNumber();
//                    }
//                }
//                if (beanShopCar.isChoose()) {
//                    holder.icon_choose_all.setTextColor(context.getResources().getColor(R.color.project_yellow));
//                } else {
//                    holder.icon_choose_all.setTextColor(context.getResources().getColor(R.color.lineColorGray));
//                }
//                sheng = price_market - price;
//                beanShopCar.setPrice(price);
//                beanShopCar.setPrice_market(price_market);
//                beanShopCar.setSheng(sheng);
//                holder.tv_price_all.setText("￥" + price);
//                holder.tv_price_market.setText("￥" + price_market);
//                holder.tv_price_market.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
//                holder.tv_price_all.setText("￥" + price);
//                holder.tv_sheng.setText("￥" + sheng);
//
////                holder.rv_list
//                holder.icon_choose_all.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        if (beanShopCar.isChoose()) {
//                            for (BeanShopCar.goods goods : arrayList_goods) {
//                                goods.setChoose(false);
//                            }
//                            beanShopCar.setChoose(false);
//                        } else {
//                            for (BeanShopCar.goods goods : arrayList_goods) {
//                                goods.setChoose(true);
//                            }
//                            beanShopCar.setChoose(true);
//                        }
//                        AdapterShopCarOutSize.this.notifyDataSetChanged();
//                    }
//                });
//                AdapterShopCarRecylerView adapter_shopcar = new AdapterShopCarRecylerView(context, arrayList_goods, fragmentShopcar);
//////                holder.rv_list.
////                holder.rv_list.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
//                holder.rv_list.setAdapter(adapter_shopcar);
////                adapter_shopcar.notifyDataSetChanged();
////                new Timer().schedule(new TimerTask() {
////                    @Override
////                    public void run() {
////                        activity.runOnUiThread(new Runnable() {
////                            @Override
////                            public void run() {
////                                SomeUtil.setListViewHeightBasedOnChildren(holder.rv_list);
////                            }
////                        });
////
////                    }
////                },500);
//                SomeUtil.setListViewHeightBasedOnChildren(holder.rv_list);
//                break;
//            case "单商品":
//                holder.ll_all.setVisibility(View.GONE);
//                holder.ll_single.setVisibility(VISIBLE);
//
//                final BeanShopCar.goods godBean = arrayList.get(i).getList().get(0);
//                holder.tv_price.setText("￥" + TextUtil.getText(godBean.getShop_price(), "0") + "");
//                if (godBean.isChoose()) {
//                    holder.icon_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
//                } else {
//                    holder.icon_choose.setTextColor(context.getResources().getColor(R.color.lineColorGray));
//                }
//                holder.tv_name.setText(TextUtil.getText(godBean.getGoods_name(), "暂无名称"));
//                holder.tv_shuxing.setText(TextUtil.getText(godBean.getSpec_name(), "暂无简介"));
//                //点击选中
//                holder.icon_choose.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        godBean.setChoose(!godBean.isChoose());
//                        AdapterShopCarOutSize.this.notifyDataSetChanged();
//                        fragmentShopcar.setMoney();
//                    }
//                });
//
//                if(godBean.isEdit()){
//                    holder.tv_del.setVisibility(VISIBLE);
//                    holder.tv_add.setVisibility(GONE);
//                    holder.tv_cut.setVisibility(GONE);
//                    holder.tv_num.setVisibility(GONE);
//                }else{
//                    holder.tv_del.setVisibility(GONE);
//                    holder.tv_add.setVisibility(VISIBLE);
//                    holder.tv_cut.setVisibility(VISIBLE);
//                    holder.tv_num.setVisibility(VISIBLE);
//                }
////                    //点击选中
////                    holder.ll_root.setOnClickListener(new View.OnClickListener() {
////                        @Override
////                        public void onClick(View view) {
////                            godBean.setChoose(!godBean.isChoose());
////                            AdapterShopCarOutSize.this.notifyDataSetChanged();
////                            fragmentShopcar.setMoney();
////                        }
////                    });
//
//
//                //减少数量
//                holder.tv_cut.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        //减少
//                        try {
//                            int num = godBean.getNumber();
//                            if (num > 1) {
//                                num--;
//                                godBean.setNumber(num);
//                            }
//
//                            holder.tv_num.setText(num + "");
//                            holder.tv_xnum.setText("x " + num);
//
//                            fragmentShopcar.setMoney();
//                        } catch (Exception e) {
//                        }
//                    }
//                });
//                //增加数量
//                holder.tv_add.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        //减少
//                        try {
//                            int num = godBean.getNumber();
//
//                            num++;
//                            godBean.setNumber(num);
//
//
//                            holder.tv_num.setText(num + "");
//                            holder.tv_xnum.setText("x " + num);
//                            fragmentShopcar.setMoney();
//                        } catch (Exception e) {
//
//                        }
//                    }
//                });
//
//                ImageUtils.BindImage(context, holder.iv_image, godBean.getOriginal_img(), 2);
//                break;
//        }
//
//        return view;
//    }
//
//
//    class Holder {
//        TextView tv_name, tv_name2, tv_price, tv_price_market, tv_sheng, tv_name_all,tv_price_all;
//        ListView rv_list;
//        IconView icon_choose, icon_choose_all;
//        ImageView iv_image;
//        TextView tv_shuxing;
//        TextView tv_cut;
//        TextView tv_num;
//        TextView tv_add;
//        TextView tv_del;
//        TextView tv_xnum;
//
//
//
//        RelativeLayout ll_single;
//        LinearLayout ll_all;
//
//        public Holder(View itemView) {
//            ll_all = itemView.findViewById(R.id.ll_all);
//            tv_name_all = itemView.findViewById(R.id.tv_name_all);
//            tv_price_all=itemView.findViewById(R.id.tv_price_all);
//            ll_single = itemView.findViewById(R.id.ll_single);
//
//            tv_name2 = itemView.findViewById(R.id.tv_name2);
//            tv_name = itemView.findViewById(R.id.tv_name);
//            tv_price = itemView.findViewById(R.id.tv_price);
//            tv_price_market = itemView.findViewById(R.id.tv_price_market);
//            tv_sheng = itemView.findViewById(R.id.tv_sheng);
//            rv_list = itemView.findViewById(R.id.rv_list);
//            icon_choose = itemView.findViewById(R.id.icon_choose);
//            icon_choose_all = itemView.findViewById(R.id.icon_choose_all);
//
//            iv_image = itemView.findViewById(R.id.iv_image);
//            tv_name = itemView.findViewById(R.id.tv_name);
//            tv_shuxing = itemView.findViewById(R.id.tv_shuxing);
//            tv_cut = itemView.findViewById(R.id.tv_cut);
//            tv_num = itemView.findViewById(R.id.tv_num);
//            tv_add = itemView.findViewById(R.id.tv_add);
//            tv_del = itemView.findViewById(R.id.tv_del);
//            tv_xnum = itemView.findViewById(R.id.tv_xnum);
//        }
//    }
//}
